package top.lshaci.learning.atguigu.interview3.aqs;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ConditionAwaitSignalDemo
 *
 * @author lshaci
 * @since 1.0.0
 */
public class ConditionAwaitSignalDemo {

    static Lock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static void main(String[] args) {
        new Thread(() -> {
            lock.lock();
            try {
                System.err.println(Thread.currentThread().getName() + " ==> IN");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.err.println(Thread.currentThread().getName() + " ==> OUT");
            } finally {
                lock.unlock();
            }
        }, "A").start();

        new Thread(() -> {
            lock.lock();
            try {
                System.err.println(Thread.currentThread().getName() + " ==> 唤醒");
                condition.signal();
            } finally {
                lock.unlock();
            }
        }, "B").start();
    }
}
